﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace XHCTMS.VIEW
{
    public partial class Performance : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Application["indexpage"] = 1;
                Application["sizepage"] = 10;
                Application["query"] = "";
                Application["count"] = GetIndex();
                if (int.Parse(Application["count"].ToString()) == 0)
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "error", "$('<div>网络连接错误</div>').dialog()", true);
                    return;
                }
                PageTotalSize.Text = Application["count"].ToString();
                RefreshData();
            }
        }

        /// <summary>
        /// 重新获取数据  并刷 新
        /// </summary>
        private void RefreshData()
        {
            PageIndex.Text = Application["indexpage"].ToString();
            Repeater1.DataSource = new BLL.Performance().View(int.Parse(Application["indexpage"].ToString()), int.Parse(Application["sizepage"].ToString()), Application["query"].ToString());
            Repeater1.DataBind();
        }

        /// <summary>
        /// 得到总页数
        /// </summary>
        /// <returns>返回int  正常/总页数  异常/0</returns>
        int GetIndex()
        {
            string TempIndex = new BLL.Performance().GetCount("");
            if (TempIndex == null)
            {
                return 0;
            }
            int index = int.Parse(TempIndex);
            if (index % int.Parse(Application["sizepage"].ToString()) == 0)
            {
                return index / int.Parse(Application["sizepage"].ToString());
            }
            else
            {
                return index / int.Parse(Application["sizepage"].ToString()) + 1;
            }
        }

        //首页
        protected void firstpage_Click(object sender, EventArgs e)
        {
            Application["indexpage"] = 1;
            RefreshData();
        }
        
        //上一页
        protected void Ononepage_Click(object sender, EventArgs e)
        {
            if (int.Parse(Application["indexpage"].ToString()) > 1)
            {
                Application["indexpage"] = int.Parse(Application["indexpage"].ToString()) - 1;
                RefreshData();
            }
        }

        //转到指定的页
        protected void Jump_Click(object sender, EventArgs e)
        {
            Application["indexpage"] = int.Parse(PageIndex.Text);
            if (int.Parse(Application["indexpage"].ToString()) >= 1 && int.Parse(PageIndex.Text) <= GetIndex())
            {
                RefreshData();
            }
        }

        //下一页
        protected void Thenextpage_Click(object sender, EventArgs e)
        { 
            if (int.Parse(Application["indexpage"].ToString()) < GetIndex())
            {
                Application["indexpage"] = int.Parse(Application["indexpage"].ToString()) + 1;
                RefreshData();
            }
        }

        //尾页
        protected void lastpage_Click(object sender, EventArgs e)
        {
            Application["indexpage"] = GetIndex();
            RefreshData();
        }

        protected void pagesizeindex_Click(object sender, EventArgs e)
        {
            Application["indexpage"] = 1;
            Application["sizepage"] = TextBoxindex.Text;
            RefreshData();
        }

    }
}